##########################################################################################

library(Seurat)
library(SeuratDisk)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--rna_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){

    rna_file <- "~/20231121_singleMuti/results/qc_atac_v3/all/testis_combined.annotationCellType.qc.Rdata"
    out_path <- "~/20231121_singleMuti/results/celltype_plot/cpdb"

}

##########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

out_path <- opt$out_path
rna_file <- opt$rna_file

dir.create(out_path , recursive = T)

##########################################################################################

a <- load(rna_file)
## scrnat
DefaultAssay(object = scrnat) <- "RNA"

##########################################################################################
## 细胞类型
grp_order2 = c("SSC",
"Differenting&Differented SPG",
"Leptotene",
"Zygotene",
"Patchytene",
"Diplotene",
"Early stage of spermatids",
"Round&ElongateS.tids",
"Sperm",
"Leydig cells",
"Myoid cells",
"Pericytes",
"Sertoli cells",
"Endothelial cells",
"NKT cells",
"Macrophages")

## 三类细胞
SSC_SPG <- c("SSC" , "Differenting&Differented SPG")
SPC <- c("Leptotene" , "Zygotene", "Patchytene",
    "Diplotene" , "Early stage of spermatids")
SPT <- c("Round&ElongateS.tids" , "Sperm")

##########################################################################################
## 预处理
## 重新构造矩阵
scRNA <- CreateSeuratObject(counts = scrnat@assays$RNA@counts)

scRNA <- NormalizeData(object = scRNA, normalization.method = 'LogNormalize',scale.factor = 10000)
scRNA <- FindVariableFeatures(object = scRNA,selection.method='vst',mean.function = ExpMean,dispersion.function = LogVMR,mean.cutoff=c(0.125,3),dispersion.cutoff=c(0.5,Inf))
scRNA <- ScaleData(scRNA,vars.to.regress = c("percent.mt","nCount_RNA"))

pcSelect=25
scRNA <- RunPCA(object=scRNA,npcs=100,pc.genes=VariableFeatures(object=scRNA))
scRNA <- RunUMAP(scRNA, dims = 1:pcSelect,min.dist = 0.5, n.neighbors = 25L)

scRNA$cell <- names(scRNA$orig.ident)
scRNA$cell_type <- scrnat$cell_type[names(scRNA$orig.ident)]

out_name <- paste0( out_path , "/scRNA.h5seurat" )
SaveH5Seurat(scRNA , filename=out_name, overwrite = TRUE)
Convert(out_name, dest = "h5ad", overwrite = TRUE)

##########################################################################################
## 生殖细胞分为三类
scrnat2 <- scRNA
scrnat2$cell_type <- ifelse(scrnat2$cell_type %in% SSC_SPG , "SSC_SPG" , scrnat2$cell_type)
scrnat2$cell_type <- ifelse(scrnat2$cell_type %in% SPC , "SPC" , scrnat2$cell_type)
scrnat2$cell_type <- ifelse(scrnat2$cell_type %in% SPT , "SPT" , scrnat2$cell_type)

out_name <- paste0( out_path , "/scRNA.combineGermToThree.h5seurat" )
SaveH5Seurat(scrnat2 , filename=out_name, overwrite = TRUE)
Convert(out_name, dest = "h5ad", overwrite = TRUE)
